/*
 * 获取ie版本
 * @return { string || number } ie版本 
 */
function getIEVersion() {
  const {userAgent} = navigator; // 取得浏览器的userAgent字符串  
  const isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1; // 判断是否IE<11浏览器  
  const isEdge = userAgent.indexOf("Edge") > -1 && !isIE; // 判断是否IE的Edge浏览器  
  const isIE11 = userAgent.indexOf("Trident") > -1 && userAgent.indexOf("rv:11.0") > -1;
  if (isIE) {
    // eslint-disable-next-line prefer-regex-literals
    const reIE = new RegExp("MSIE (\\d+\\.\\d+);");
    reIE.test(userAgent);
    const fIEVersion = parseFloat(RegExp.$1);
    if (fIEVersion === 7) {
        return 7;
    } if (fIEVersion === 8) {
        return 8;
    } if (fIEVersion === 9) {
        return 9;
    } if (fIEVersion === 10) {
        return 10;
    } 
       return 6; // IE版本<=7
      
  } if (isEdge) {
    return "edge"; // edge
  } if (isIE11) {
    return 11; // IE11  
  } 
    return -1; // 不是ie浏览器
    
}

/*
 * 获取文件数据
 * @param { string } url ,路径
 * @return { string } text ,文件内容
 */
export async function getJson(url) {
  // console.log(IEVersion())
  if (getIEVersion() > 0) {
    // ie浏览器
    return new Promise((resolve, reject) => {
      const xhr = new XMLHttpRequest();
      xhr.onload = e => {
          resolve(e.target.responseText);
      };
      xhr.onerror = err=> {
          reject(err);
      };
      xhr.open("GET", url, true);
      xhr.send(null);


    });
  } 
  const init = {
    methods: "GET",
    cache: "default", // 使用缓存
    // credentials: "include",//任何请求都携带cookie
  };
  return await fetch(url, init).then(response => {
    if (response.ok) { // http状态码为200-299
        return response.json();
    } 
    console.error(`文件路径错误：${url}`);
      

  });
    

}

/**
 * 将tree转换为label-value格式
 * @param {*} arr 
 * @param {*} name 
 * @param {*} id 
 * @returns 
 */
export function setLabel(arr = [], name = "name", id = "id") {
  for (let i = 0; i < arr.length; i += 1) {
    arr[i].title = arr[i][name];
    arr[i].label = arr[i][name];
    arr[i].value = arr[i][id];
    arr[i].key = arr[i][id];
    arr[i].isLeaf = arr[i].leafNode === 1;
    if (arr[i].children) {
      setLabel(arr[i].children);
    }
  }
  return arr;
}



function add0(m) {
  return m < 10 ? `0${  m}` : m;
}

/**
 * 将时间戳转换为日期
 * @param {*} timestamp
 * @param {*} isYMD 是否只需要年月日
 * @returns
 */
export function format(timestamp, isYMD) {
  const time = new Date(timestamp);
  const y = time.getFullYear();
  const m = time.getMonth() + 1;
  const d = time.getDate();
  const h = time.getHours();
  const mm = time.getMinutes();
  const s = time.getSeconds();
  return isYMD ? `${y  }-${  add0(m)  }-${  add0(d)}` : `${y  }-${  add0(m)  }-${  add0(d)  } ${  add0(h)  }:${  add0(mm)  }:${  add0(s)}`;
}

/**
 * 取url里的参数
 * @param {*} name 
 * @param {*} url 
 * @returns 
 */
export function getParameterByName(name, url) {
  if (!url) {
      url = window.location.href;
  }
  // eslint-disable-next-line no-empty-character-class
  name =name.replace(/[]/g, "\\$&");  // 这句老是出错，猜测是用来去掉一些斜线的，先注释掉

  const regex = new RegExp(`[?&]${  name  }(=([^&#]*)|&|#|$)`);
      const results = regex.exec(url);
  if (!results) return null;
  if (!results[2]) return"";
  return decodeURIComponent(results[2].replace(/\+/g, " "));
}


/**
 * 生成指定区间的随机整数
 * @param {number} min
 * @param {number} max
 * @returns {number}
 */
export function randomNum (min, max) {
  return Math.floor(Math.random() * (max - min) + min);
};